package com.shanghaiyimu.yimiyangche.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.GoodsListAdapter;
import com.shanghaiyimu.yimiyangche.bean.OilBen;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class GoodsListActivity extends AppCompatActivity implements View.OnClickListener{

    private ListView listView;
    private List<OilBen> datas = new ArrayList<>();
    private GoodsListAdapter adapter;
    private OilBen goodsBean ;
    private TextView textView1,textView2,textView3,textView4;
    private boolean first = false;
    private boolean first2 = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_list);
        initView();
        initData();
    }

    private void initData() {
        for (int i = 0; i < 10; i++) {
            goodsBean = new OilBen();
            goodsBean.setName("机油"+i);
            goodsBean.setKucun(i+100);
            goodsBean.setPingjia(99-i);
            goodsBean.setPrice(78+i);
            goodsBean.setXiaoliang(i+1000);

            datas.add(goodsBean);
        }
        adapter.notifyDataSetChanged();
    }

    private void initView() {
        listView = (ListView) findViewById(R.id.goods_list_lv);
        adapter = new GoodsListAdapter(this,datas);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(GoodsListActivity.this, GoodsContentActivity.class);
                intent.putExtra("name","qwe");
                startActivity(intent);
            }
        });

        textView1 = (TextView) findViewById(R.id.goods_list_txt11);
        textView2 = (TextView) findViewById(R.id.goods_list_txt2);
        textView3 = (TextView) findViewById(R.id.goods_list_txt3);
        textView4 = (TextView) findViewById(R.id.goods_list_txt4);
        textView1.setOnClickListener(this);
        textView2.setOnClickListener(this);
        textView3.setOnClickListener(this);
        textView4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        setClean();
        switch (v.getId()){
            case R.id.goods_list_txt11:
                textView1.setTextColor(Color.RED);
                break;
            case R.id.goods_list_txt2:
                textView2.setTextColor(Color.RED);
                //价格
                if (!first) {
                    Collections.sort(datas, new Comparator<OilBen>() {
                        @Override
                        public int compare(OilBen o1, OilBen o2) {
                            if (o1.getPrice() > o2.getPrice()) {
                                return -1;
                            }
                            if (o1.getPrice() == o2.getPrice()) {
                                return 0;
                            } else {
                                return 1;
                            }
                        }
                    });
                    adapter.notifyDataSetChanged();
                    first = true;
                }else {
                    Collections.sort(datas, new Comparator<OilBen>() {
                        @Override
                        public int compare(OilBen o1, OilBen o2) {
                            if (o1.getPrice() > o2.getPrice()) {
                                return 1;
                            }
                            if (o1.getPrice() == o2.getPrice()) {
                                return 0;
                            } else {
                                return -1;
                            }
                        }
                    });
                    adapter.notifyDataSetChanged();
                    first = false;
                }
                break;
            case R.id.goods_list_txt3:
                //销量
                textView3.setTextColor(Color.RED);
                if (!first2) {
                    Collections.sort(datas, new Comparator<OilBen>() {
                        @Override
                        public int compare(OilBen o1, OilBen o2) {
                            if (o1.getXiaoliang() > o2.getXiaoliang()) {
                                return -1;
                            }
                            if (o1.getXiaoliang() == o2.getXiaoliang()) {
                                return 0;
                            } else {
                                return 1;
                            }
                        }
                    });
                    adapter.notifyDataSetChanged();
                    first2 = true;
                }else {
                    Collections.sort(datas, new Comparator<OilBen>() {
                        @Override
                        public int compare(OilBen o1, OilBen o2) {
                            if (o1.getXiaoliang() > o2.getXiaoliang()) {
                                return 1;
                            }
                            if (o1.getXiaoliang() == o2.getXiaoliang()) {
                                return 0;
                            } else {
                                return -1;
                            }
                        }
                    });
                    adapter.notifyDataSetChanged();
                    first2 = false;
                }
                break;
            case R.id.goods_list_txt4:
                textView4.setTextColor(Color.RED);
                break;
        }
    }
    private void setClean(){
        textView1.setTextColor(Color.GRAY);
        textView2.setTextColor(Color.GRAY);
        textView3.setTextColor(Color.GRAY);
        textView4.setTextColor(Color.GRAY);
    }
}
